#include "FKLogger.h"

#include "FKEvent.h"
#include "FKRoom.h"
#include "FKObjectsData.h"

#include <QDataStream>

#include "FKEventNames.h"

FKEvent::FKEvent(int dummy1, int dummy2, qint32 dummy3):
    isPropertyNotifier(false), _object(-1),_subject(QString()),_value(QVariant()),_client(-1),_owner(-1),_emitter(-1){
    FK_CBEGIN
    Q_UNUSED(dummy1)
    Q_UNUSED(dummy2)
    Q_UNUSED(dummy3)
    FK_CEND
}

FKEvent::FKEvent(qint32 object, QString subject, QVariant value, qint32 reciever, qint32 client, qint32 owner, qint32 emitter):
    FKSystemObject(),isPropertyNotifier(false),_object(object),_subject(subject),_value(value),_client(client),_owner(owner),_emitter(emitter){
    FK_CBEGIN
    _recievers.append(reciever);
    FK_CEND
}

FKEvent::FKEvent(qint32 object, QString subject, QVariant value, QList<qint32> recievers, qint32 owner):
    FKSystemObject(),isPropertyNotifier(false),_object(object),_subject(subject),_value(value),_recievers(recievers),_client(-1),_owner(owner),_emitter(-1){
    FK_CBEGIN
    FK_CEND
}

FKEvent::~FKEvent(){
    FK_DBEGIN
    FK_DEND
}

qint32 FKEvent::object() const{
    return _object;
}

QString FKEvent::subject() const{
    return _subject;
}

QVariant FKEvent::value() const{
    return _value;
}

QList<qint32> FKEvent::recievers() const{
    return _recievers;
}

void FKEvent::pack(QDataStream &stream){
    FKSystemObject::pack(stream);
    stream<<_object;
    stream<<_subject;
    stream<<_value;
    stream<<_client;
    stream<<_owner;
    stream<<_emitter;
    stream<<isPropertyNotifier;
}

bool FKEvent::load(QDataStream &stream){
    FKSystemObject::load(stream);
    stream>>_object;
    stream>>_subject;
    stream>>_value;
    stream>>_client;
    stream>>_owner;
    stream>>_emitter;
    stream>>isPropertyNotifier;
    return true;
}

qint32 FKEvent::client() const{
    return _client;
}

qint32 FKEvent::owner() const{
    return _owner;
}

qint32 FKEvent::emitter() const{
    return _emitter;
}

FKEvent* FKEvent::errorEvent(FKEvent* errorSource){
    FKEvent* e=new FKEvent(errorSource->client(),FKEventName::g_error,errorSource->subject(),errorSource->client(),errorSource->client(),errorSource->owner(),errorSource->object());
    FK_MLOGV("Error generated from source",errorSource->subject())
    return e;
}

FKEvent* FKEvent::errorEvent(QString message, qint32 clientID){
    FKEvent* e= new FKEvent(clientID,FKEventName::g_error,message,clientID,clientID,clientID,clientID);
    e->_recievers.append(-2);
    FK_MLOGV("Error generated",message)
    return e;
}

FKEvent* FKEvent::createObjectsEvent(qint32 target, QSet<FKObject*> objects){
    return makeEvent(target,FKEventName::g_createObjects,QVariant::fromValue(FKObjectsData(objects)),target);
}

FKEvent* FKEvent::deleteObjectsEvent(qint32 target, QSet<FKObject *> objects){
    QList<qint32> lst;
    foreach(FKObject* obj, objects)lst.append(obj->getID());
    return makeEvent(target,FKEventName::g_deleteObjects,QVariant::fromValue(lst),target);
}

FKEvent* FKEvent::makeAction(qint32 object, QString subject, QVariant value, qint32 client, qint32 owner, qint32 emitter){
    FK_MLOGV("action created",QString::number(object)+subject)
    FK_MLOGV("action value",value)
    return new FKEvent(object,subject,value,-1,client,owner,emitter);
}

FKEvent* FKEvent::makeEvent(qint32 object, QString subject, QVariant value, QList<qint32> recievers, qint32 owner){
    FK_MLOGV("event created",subject)
    return new FKEvent(object,subject,value,recievers,owner);
}

FKEvent* FKEvent::makeEvent(qint32 object, QString subject, QVariant value, qint32 reciever, qint32 owner){
    FK_MLOGV("event created",subject)
    return new FKEvent(object,subject,value,reciever,-1,owner,-1);
}
